package features.advance.jdk.jvm;

import redis.clients.jedis.Jedis;

import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author LIN
 * @date 2021-07-13
 */
public class ClassLoaderDemo extends ClassLoader implements Serializable {
    public static void main(String[] args) throws Exception {
        new ClassLoaderDemo().bar();
        Class<?> aClass = Class.forName("features.advance.jdk.io.mina.Demo");
        Object o = aClass.newInstance();
        Method test = o.getClass().getDeclaredMethod("test", null);
        test.invoke(o,null);

        Lock demo = Lock.getInstance();
//        讲对象存到文件中
        String fileName = "D:\\files\\zkr_micro_daily\\Core-Java\\advance\\src\\main\\java\\features\\advance\\jdk\\jvm\\demo";
        FileHelper fileHelper = new FileHelper(fileName);
        File file = fileHelper.saveObjToFile(demo);
        int mark = -1;
        FileInputStream stream = new FileInputStream(file);
        StringBuilder sb = new StringBuilder();
        while((mark = stream.read())!= -1){
            sb.append(mark);
        }
        Jedis jedis = new Jedis("10.133.79.86",6379);
        jedis.sadd(file.getName(),sb.toString());
        jedis.close();


        Lock objFromFile = fileHelper.getObjFromFile();



        // 锁机制
//        new Thread(() -> demo.executeAB()).start();
//        new Thread(() -> demo.callAB()).start();
        new Thread(()->demo.get()).start();

        new Thread(()->{
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erpcg_pd?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8");
                demo.close(conn);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();



    }
    public void bar() throws NoSuchMethodException {
        bar2();
    }

    public void bar2() throws NoSuchMethodException {
        System.out.println(this.getClass().getMethod("bar2",null).getName());
    }




}
